package com.bjpowernode.boss.mvc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjpowernode.boss.domain.PayProduct;
import com.bjpowernode.boss.domain.PayWay;
import com.bjpowernode.boss.enums.ajax.Code;
import com.bjpowernode.boss.enums.buz.PublicStatus;
import com.bjpowernode.boss.exception.BossException;
import com.bjpowernode.boss.mapper.PayProductMapper;
import com.bjpowernode.boss.mapper.PayWayMapper;
import com.bjpowernode.boss.mvc.model.query.PayProductQuery;
import com.bjpowernode.boss.mvc.service.PayProductService;
import com.bjpowernode.boss.utils.NoUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class PayProductServiceImpl extends ServiceImpl<PayProductMapper, PayProduct> implements PayProductService {

    @Resource
    private PayProductMapper payProductMapper;

    @Resource
    private PayWayMapper payWayMapper;

    @Override
    public IPage<PayProduct> selectProductTablePageList(Integer pageNo, int pageSize) {
        // 查询数据
        return payProductMapper.selectPage(new Page<>(pageNo, pageSize), new QueryWrapper<PayProduct>().orderByAsc("edit_time"));
    }

    @Override
    public void addProduct(PayProductQuery payProductQuery) {
        // 数据处理
        String productCode = payProductQuery.getProductCode().trim();
        String productName = payProductQuery.getProductName().trim();
        // 先分别查询 值在数据库当中是否存在
        PayProduct payProduct = payProductMapper.selectOne(new LambdaQueryWrapper<PayProduct>().eq(PayProduct::getProductCode, productCode));
        if (payProduct != null) {
            throw new BossException(Code.PAY_PRODUCT_CODE_EXIST);
        }
        payProduct = payProductMapper.selectOne(new LambdaQueryWrapper<PayProduct>().eq(PayProduct::getProductName, productName));
        if (payProduct != null) {
            throw new BossException(Code.PAY_PRODUCT_NAME_EXIST);
        }
        // 添加数据
        payProduct = new PayProduct();
        payProduct.setId(NoUtil.uuid());
        payProduct.setProductCode(productCode);
        payProduct.setProductName(productName);
        payProduct.setStatus(PublicStatus.ENABLE.name());
        payProduct.setEditTime(new Date());
        payProductMapper.insert(payProduct);
    }

    @Override
    public List<PayProduct> selectProductCodeAndName() {
        return payProductMapper
                .selectList(new QueryWrapper<PayProduct>().select("product_code", "product_name").orderByAsc("product_code").eq("status", PublicStatus.ENABLE.name()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeProductByCode(String productCode) {
        // 删除 支付产品
        int delete = payProductMapper.delete(new LambdaQueryWrapper<PayProduct>().eq(PayProduct::getProductCode, productCode));
        // 判断是否删除成功
        if (delete == 0) {
            return;
        }
        // 删除 支付方式
        payWayMapper.delete(new LambdaQueryWrapper<PayWay>().eq(PayWay::getPayProductCode, productCode));
    }

    @Override
    public void updateProductStatus(String productCode) {
        // 获取该 编号原来数据 对应的 信息
        PayProduct payProduct = payProductMapper.selectOne(new LambdaQueryWrapper<PayProduct>().eq(PayProduct::getProductCode, productCode));
        // 判断是否存在
        if (payProduct == null) {
            throw new BossException(Code.PAY_PRODUCT_NOT_FOUNT);
        }

        LambdaUpdateWrapper<PayProduct> updateWrapper = new LambdaUpdateWrapper<>();
        String newStatus = PublicStatus.ENABLE.name();
        if (newStatus.equals(payProduct.getStatus())) {
            // 相反
            newStatus = PublicStatus.DISABLE.name();
        }
        updateWrapper.set(PayProduct::getStatus, newStatus);
        updateWrapper.eq(PayProduct::getProductCode, productCode);
        payProductMapper.update(updateWrapper);
    }

    @Override
    public List<PayProduct> matchFindProductInfoList(String productName) {
        return payProductMapper.selectList(new LambdaQueryWrapper<PayProduct>()
                .like(PayProduct::getProductName, productName)
                .select(PayProduct::getProductCode, PayProduct::getProductName));
    }
}
